/* Includes ------------------------------------------------------------------*/
#include "toast.h"
#include "puresun_func.h"

/* Private macro -------------------------------------------------------------*/

/* Private types -------------------------------------------------------------*/
typedef struct
{
    lv_obj_t **toast;
    lv_font_t *font;
} toast_t;

/* Private variables ---------------------------------------------------------*/
toast_t toast_local = {NULL, NULL};
string toast_text = NULL;
int toast_width = 0;

/* Private function prototypes -----------------------------------------------*/
void toast_anmin(lv_obj_t *obj, int32_t value);
void show_toast_o_cb(lv_timer_t *timer);

/* Private functions ---------------------------------------------------------*/

/******************************************************************************
 * Function Name  : toast_init
 * Description    : toast init
 * Input          : [lv_obj_t **toast, lv_font_t *font] toast object and font
 *                  - [lv_obj_t **toast] toast object
 *                  - [lv_font_t *font] font
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void toast_init(lv_obj_t **toast, lv_font_t *font)
{
    toast_local.toast = toast;
    toast_local.font = font;
}

/******************************************************************************
 * Function Name  : toast_anmin
 * Description    : toast animation
 * Input          : [lv_obj_t *obj, int32_t value] object and value
 *                  - [lv_obj_t *obj] object
 *                  - [int32_t value] value
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void toast_anmin(lv_obj_t *obj, int32_t value)
{
    lv_obj_set_style_opa(obj, (lv_opa_t)(2.3 * value), 0);
    lv_obj_set_y(obj, value / 2);
}

/******************************************************************************
 * Function Name  : show_toast
 * Description    : show toast
 * Input          : [lv_obj_t **toast, char *text, int width, lv_font_t *font] toast object, text, width and font
 *                  - [lv_obj_t **toast] toast object
 *                  - [char *text] text
 *                  - [int width] width
 *                  - [lv_font_t *font] font, if NULL, use default font
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void show_toast(lv_obj_t **toast, char *text, int width, lv_font_t *font)
{
    width = pf_min(width, LV_HOR_RES_MAX * 0.8);
    if (*toast != NULL)
    {
        lv_obj_del(*toast);
    }

    *toast = lv_obj_create(lv_scr_act());
    lv_obj_set_size(*toast, width, 40);
    lv_obj_set_style_pad_all(*toast, 0, 0);
    lv_obj_set_style_bg_color(*toast, lv_color_hex(0x000000), 0);
    lv_obj_set_style_bg_opa(*toast, LV_OPA_COVER, 0);
    lv_obj_set_style_text_color(*toast, lv_color_hex(0xFFFFFF), 0);
    lv_obj_set_style_pad_all(*toast, 10, 0);
    lv_obj_set_style_radius(*toast, 5, 0);
    lv_obj_set_style_border_width(*toast, 0, 0);
    lv_obj_set_scrollbar_mode(*toast, LV_SCROLLBAR_MODE_OFF);
    lv_obj_align_to(*toast, lv_scr_act(), LV_ALIGN_TOP_MID, 0, 50);

    lv_obj_t *label = lv_label_create(*toast);
    if (font != NULL)
    {
        lv_obj_set_style_text_font(label, font, 0);
    }
    lv_label_set_text(label, text);
    lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, *toast);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)toast_anmin);
    lv_anim_set_values(&a, 0, 100);
    lv_anim_set_time(&a, 500);
    lv_anim_set_path_cb(&a, lv_anim_path_ease_out);
    lv_anim_set_playback_delay(&a, 1000);
    lv_anim_set_playback_time(&a, 500);
    lv_anim_start(&a);
}

/******************************************************************************
 * Function Name  : show_toast_e
 * Description    : show toast easily
 * Input          : [char *text, int width] text and width
 *                  - [char *text] text
 *                  - [int width] width
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void show_toast_e(char *text, int width)
{
    if (toast_local.toast != NULL)
    {
        show_toast(toast_local.toast, text, width, toast_local.font);
    }
}

/******************************************************************************
 * Function Name  : show_toast_o
 * Description    : show toast easily by other thread
 * Input          : [char *text, int width] text and width
 *                  - [char *text] text
 *                  - [int width] width
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void show_toast_o(char *text, int width)
{
    lv_timer_t *timer = lv_timer_create(show_toast_o_cb, 0, NULL);
    lv_timer_set_repeat_count(timer, 1);
    toast_text = text;
    toast_width = width;
}

/******************************************************************************
 * Function Name  : show_toast_o_cb
 * Description    : show toast easily by other thread callback
 * Input          : [lv_timer_t *timer] timer
 *                  - [lv_timer_t *timer] timer
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void show_toast_o_cb(lv_timer_t *timer)
{
    show_toast_e(toast_text, toast_width);
}

/************************ (C) COPYRIGHT PURESUN *****END OF FILE***************/
